/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.panels.graphical.drawables;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.util.Vector;
import jgf.Attribute;
import jgf.beans.NodeBean;

/**
 * 
 * @author Marcos Garcia Ortega <a href="mailto:thiamath@gmail.com">&lt;thiamath@gmail.com&gt;</a>
 */
public class DrawNode implements Drawable, Interactive {

	protected Integer index;
	public static float RADIUS = 20;
	protected Ellipse2D.Float shape;
	protected Color background = Color.WHITE;
	protected Color foreground = Color.BLACK;
	protected Vector<DrawArc> linked;
	protected Attribute showedAttribute;
	protected String label;
	protected String tag;
	protected boolean selected = false;

	/**
	 * This constructor should never be called. Use <code>DrawNode(Node node, Point coords)</code> instead.
	 */
	public DrawNode() {
	}

	public DrawNode(NodeBean node, Point coords) {
		this.linked = new Vector<DrawArc>();
		this.shape = new Ellipse2D.Float();
		this.setDefaultRadius();
		this.label = node.getLabel();
		if (!node.getAttributes().isEmpty())
			this.showedAttribute = node.getAttributes().get(0);
		//if(showedAttribute != null)
		//	tag = showedAttribute.getName() + "=" + showedAttribute.getValue();
		this.index = node.getId();
		this.setCenter(coords);
	}

	public boolean isSelected() {
		return selected;
	}

	public void select() {
		selected = true;
		this.background = Color.RED;
	}

	public void deselect() {
		selected = false;
		this.background = Color.WHITE;
	}

	public void setCenter(float x, float y) {
		shape.setFrameFromCenter(x, y, shape.height / 2, shape.width / 2);
		for (DrawArc drawArc : linked) {
			if (drawArc.getFrom() == this.getId()) {
				drawArc.setOrigin(x, y);
			} else {
				drawArc.setEnd(x, y);
			}
		}
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public Attribute getShowedAttribute() {
		return showedAttribute;
	}

	public void setShowedAttribute(Attribute showedAttribute) {
		this.showedAttribute = showedAttribute;
		if(showedAttribute != null)
			tag = showedAttribute.getName() + "=" + showedAttribute.getValue();
	}

	public void setCenter(Point center) {
		setCenter(center.x, center.y);
		setDefaultRadius();
	}

	public void setDefaultRadius() {
		shape.setFrameFromCenter(shape.getCenterX(), shape.getCenterY(), shape.getCenterX() + RADIUS, shape.getCenterY() + RADIUS);
	}

	public Point getCenter() {
		Point ret = new Point();
		ret.setLocation(shape.getCenterX(), shape.getCenterY());

		return ret;
	}

	public double getCenterX() {
		return shape.getCenterX();
	}

	public double getCenterY() {
		return shape.getCenterY();
	}

	public void display(Graphics2D g) {
		g.setColor(background);
		g.fill(shape);
		g.setColor(foreground);
		g.draw(shape);
		g.drawString(this.getId().toString()//getTag()
				, (float) shape.getCenterX(), (float) shape.getCenterY());
	}

	public Ellipse2D.Float getShape() {
		return shape;
	}

	public void setColor(Color color) {
		this.background = color;
	}

	public void setLinkedArcs(Vector<DrawArc> linkedArcs) {
		for (DrawArc drawArc : linkedArcs) {
			addLinkedArc(drawArc);
		}
	}

	public void addLinkedArc(DrawArc arc) {
		linked.add(arc);
		if (arc.getFrom().equals(this.getId())) {
			arc.setOrigin(this.getCenter());
		} else {
			arc.setEnd(this.getCenter());
		}
	}

	public Vector<DrawArc> getLinkedArcs() {
		return linked;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public Integer getId() {
		return index;
	}

}
